package com.example.administrator.guosaicheku_13;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.guosaicheku_13.util.savechewei;
import com.nle.mylibrary.enums.led.PlayType;
import com.nle.mylibrary.enums.led.ShowSpeed;
import com.nle.mylibrary.forUse.led.LedScreen;
import com.nle.mylibrary.forUse.mdbus4150.MdBus4150SensorListener;
import com.nle.mylibrary.forUse.mdbus4150.Modbus4150;
import com.nle.mylibrary.transfer.DataBusFactory;

public class MainActivity extends AppCompatActivity {
    private TextView textView2;
    private ImageView imageView;
    private Modbus4150 modbus4150;
    private LedScreen ledScreen;
    private Integer i=0,current=0;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        Mythread mythread=new Mythread();
        mythread.start();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,setting.class);
                startActivity(intent);
            }
        });
    }
    private void  init(){
        textView2=findViewById(R.id.textView2);
        imageView=findViewById(R.id.car);
        modbus4150=new Modbus4150(DataBusFactory.newSocketDataBus("172.21.2.201",4196));
        ledScreen=new LedScreen(DataBusFactory.newSocketDataBus("172.21.2.203",4196));
        button=findViewById(R.id.setting);
    }
    private class  Mythread extends Thread{
        @Override
        public void run() {
            super.run();
            while (true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String s = savechewei.get(MainActivity.this);

                if(s!=null){
                    i=Integer.parseInt(s);
                    try {
                        modbus4150.getVal(0, new MdBus4150SensorListener() {
                            @Override
                            public void onVal(int val) {

                                if(val==1&&current<=i+1){
                                    current+=1;
                                    try {
                                        ledScreen.sendTxt("已用："+current+"剩余数量："+(i-current),PlayType.LEFT,ShowSpeed.SPEED1,1,100);
                                        Animation animation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.tra);
                                        imageView.startAnimation(animation);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }else if(current>i){
                                    try {
                                        ledScreen.sendTxt("满了满了",PlayType.LEFT,ShowSpeed.SPEED1,1,100);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }if(val==0){
                                    Log.i("main",val+"");
                                }
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView2.setText((i-current)+"个");
                        }
                    });
                }
            }
        }
    }
}
